package com.example.xmpic.ui.activity.setting;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.example.common.base.BaseBindActivity;
import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivityChangePwdBinding;
import com.example.xmpic.support.FunDevicePassword;
import com.example.xmpic.support.FunError;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.OnFunDeviceOptListener;
import com.example.xmpic.support.config.ModifyPassword;
import com.example.xmpic.support.models.FunDevice;
import com.lib.FunSDK;
import com.lib.sdk.struct.H264_DVR_FILE_DATA;

/**
 * @author skygge
 * @date 2019-11-27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置/修改密码
 */
public class CameraSetPwdActivity extends BaseBindActivity<ActivityChangePwdBinding> implements OnFunDeviceOptListener {

    private FunDevice mFunDevice = null;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_change_pwd;
	}

	@Override
	protected void initialize() {
		super.initialize();
		int deviceId = getIntent().getIntExtra("FUN_DEVICE_ID", 0);
		mFunDevice = FunSupport.getInstance().findDeviceById(deviceId);
		if ( null == mFunDevice ) {
			finish();
			return;
		}
		FunSupport.getInstance().registerOnFunDeviceOptListener(this); //注册设备操作类的监听
	}

	@Override
	protected void initView() {
		super.initView();
		mDBind.etNewPassword.setInputType(1);
		mDBind.etConfirmPassword.setInputType(1);
		mDBind.btChangePassword.setEnabled(false);
	}

	@Override
	protected void initData() {
		super.initData();
		if (TextUtils.isEmpty(mFunDevice.loginPsw)){
			mDBind.etOldPassword.setVisibility(View.GONE);
			mDBind.viewLine.setVisibility(View.GONE);
		}
	}

	@Override
	protected void initListener() {
		super.initListener();
		mDBind.ivPwdBack.setOnClickListener(view -> finish());
		mDBind.btChangePassword.setOnClickListener(view -> tryToChangePwd());
		mDBind.etConfirmPassword.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				textChange();
			}
		});
	}

	private void textChange(){
		if (!TextUtils.isEmpty(mFunDevice.loginPsw)){
			if (mDBind.etOldPassword.getInputContent().length()>0
					&& mDBind.etNewPassword.getInputContent().length()>0
					&& mDBind.etConfirmPassword.getInputContent().length()>0){
				mDBind.btChangePassword.setBackgroundResource(R.drawable.btn_gradient_bg);
				mDBind.btChangePassword.setEnabled(true);
			}else {
				mDBind.btChangePassword.setBackgroundResource(R.drawable.btn_login_empty_bg);
				mDBind.btChangePassword.setEnabled(false);
			}
		}else {
			if (mDBind.etNewPassword.getInputContent().length()>0
					&& mDBind.etConfirmPassword.getInputContent().length()>0){
				mDBind.btChangePassword.setBackgroundResource(R.drawable.btn_gradient_bg);
				mDBind.btChangePassword.setEnabled(true);
			}else {
				mDBind.btChangePassword.setBackgroundResource(R.drawable.btn_login_empty_bg);
				mDBind.btChangePassword.setEnabled(false);
			}
		}
	}

    private void tryToChangePwd() {
        String oldPwd = mDBind.etOldPassword.getInputContent();
        String newPwd = mDBind.etNewPassword.getInputContent();
        String newPwdConfirm = mDBind.etConfirmPassword.getInputContent();

        if (!newPwd.equals(newPwdConfirm)) {
            showToast(getString(R.string.user_change_password_error_passwnotequal));
            return;
        }
        showProgressDialog();

        // 修改密码,设置ModifyPassword参数
        // 注意,如果是直接调用FunSDK.DevSetConfigByJson()接口,需要对密码做MD5加密,参考ModifyPassword.java的处理
        ModifyPassword modifyPwd = new ModifyPassword();
		modifyPwd.PassWord = oldPwd;
		modifyPwd.NewPassWord = newPwdConfirm;
        
        FunSupport.getInstance().requestDeviceSetConfig(mFunDevice, modifyPwd);
    }

	@Override
	public void onDeviceLoginSuccess(FunDevice funDevice) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceLoginFailed(FunDevice funDevice, Integer errCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceGetConfigSuccess(FunDevice funDevice,
										 String configName, int nSeq) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceGetConfigFailed(FunDevice funDevice, Integer errCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceSetConfigSuccess(FunDevice funDevice, String configName) {
		if ( ModifyPassword.CONFIG_NAME.equals(configName) ) {
			// 修改密码成功,保存新密码,下次登录使用
			if (null != mFunDevice) {
				FunDevicePassword.getInstance().saveDevicePassword(mFunDevice.getDevSn(), mDBind.etNewPassword.getInputContent());
			}
			// 库函数方式本地保存密码
			if (FunSupport.getInstance().getSaveNativePassword()) {
				FunSDK.DevSetLocalPwd(mFunDevice.getDevSn(), "admin", mDBind.etNewPassword.getInputContent());
				// 如果设置了使用本地保存密码，则将密码保存到本地文件
			}
			// 隐藏等待框
			dismissProgressDialog();
			showToast(getString(R.string.user_forget_pwd_reset_passw_success));
			finish();
		}
	}

	@Override
	public void onDeviceSetConfigFailed(FunDevice funDevice, String configName, Integer errCode) {
		dismissProgressDialog();
		showToast(FunError.getErrorStr(errCode));
	}

	@Override
	public void onDeviceChangeInfoSuccess(FunDevice funDevice) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceChangeInfoFailed(FunDevice funDevice, Integer errCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceOptionSuccess(FunDevice funDevice, String option) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceOptionFailed(FunDevice funDevice, String option,
                                     Integer errCode) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceFileListChanged(FunDevice funDevice) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceFileListChanged(FunDevice funDevice,
			H264_DVR_FILE_DATA[] datas) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDeviceFileListGetFailed(FunDevice funDevice) {
		// TODO Auto-generated method stub
		
	}


	@Override
	protected void onDestroy() {
		// 注销监听
		super.onDestroy();
		FunSupport.getInstance().removeOnFunDeviceOptListener(this);
	}
}
